using System;
using System.Collections;
using System.Data;
using CE.Database;
using CE.Security;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.User
{
    public class ProfileDB
    {
        public ProfileDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(Profile profile)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCXTHReportsUserProfileInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@UserId", profile.UserId);
            //_sPara.Add("@Salutation", profile.Salutation);
            _sPara.Add("@FirstName", profile.FirstName == string.Empty ? string.Empty : profile.FirstName);
            _sPara.Add("@SurName", profile.SurName == string.Empty ? string.Empty : profile.SurName);
            _sPara.Add("@DOB", profile.DOB);
            _sPara.Add("@Gender", profile.Gender);
            _sPara.Add("@MaritalStatus", profile.MaritalStatus);
            //_sPara.Add("@Nationality", profile.Nationality == string.Empty ? string.Empty : profile.Nationality);
            _sPara.Add("@Status", profile.Status);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public Profile GetProfile(Int64 UserId)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocCXTHUserProfileSelectSingleItem";
            Profile _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@UserId", UserId);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new Profile();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }
                
        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

        private Profile FillDataRecord(DataTable myDataTable)
        {
            Profile myProfile = new Profile();
            myProfile.ProfileId = Convert.ToInt64(myDataTable.Rows[0]["ProfileId"].ToString());
            myProfile.UserId = Convert.ToInt64(myDataTable.Rows[0]["UserId"].ToString());
            //myProfile.Salutation = myDataTable.Rows[0]["Salutation"].ToString();
            string _firstname = myDataTable.Rows[0]["FirstName"].ToString();
            if (_firstname != string.Empty)
            {
                myProfile.FirstName = _firstname;
            }

            string _surname = myDataTable.Rows[0]["SurName"].ToString();
            if (_surname != string.Empty)
            {
                myProfile.SurName = _surname;
            }
            myProfile.DOB = Convert.ToDateTime(myDataTable.Rows[0]["DOB"].ToString());
            myProfile.Gender = Convert.ToChar(myDataTable.Rows[0]["Gender"].ToString());
            myProfile.MaritalStatus = Convert.ToInt32(myDataTable.Rows[0]["MaritalStatus"].ToString());
            //string _nationality = myDataTable.Rows[0]["Nationality"].ToString();
            //if (_nationality != string.Empty)
            //{
            //    myProfile.Nationality = _nationality;
            //}
            myProfile.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            return myProfile;
        }
        
    }
}